package com.midea.ai.air.ui.device.product.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.Constant;

/**
 * 自定义titlebar，单品的公用titleview
 *
 * @author chuo.qin 2016/3/22
 */

public class TitleBarView extends FrameLayout
{
    private RelativeLayout mainView;
    private TextView text_title;
    private RadioButton button_left;
    private TextView    button_right;

    public TitleBarView(Context context)
    {
        this(context, null, 0);
    }

    public TitleBarView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context)
    {
        View.inflate(context, R.layout.fragment_device_title_bar, this);
        FrameLayout mBarLayout = (FrameLayout)findViewById(R.id.bar_parent);
        ViewGroup.LayoutParams LP = mBarLayout.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        mBarLayout.setLayoutParams(LP);

        this.mainView = (RelativeLayout) findViewById(R.id.device_bar_mainview);
        this.text_title = (TextView) findViewById(R.id.device_bar_title);
        this.button_left = (RadioButton) findViewById(R.id.back_button);
        this.button_right = (TextView)findViewById(R.id.tv_action_bar_right);

        int width = getResources().getDimensionPixelOffset(R.dimen.height_12dp);
        int height = getResources().getDimensionPixelOffset(R.dimen.height_22dp);
        setDrawableLeft(button_left, R.drawable.air_action_bar_left_white, width, height);
    }

    /**
     * 设置图标
     *
     * @param radioButton
     * @param drawableId
     * @param width
     * @param height
     */
    public void setDrawableLeft(RadioButton radioButton, int drawableId, int width, int height) {
        Drawable drawable = getResources().getDrawable(drawableId);
        drawable.setBounds(0, 0, width, height);
        radioButton.setCompoundDrawables(drawable, null, null, null);
    }

    // 标题
    public void setTitleText(int resId)
    {
        this.text_title.setText(resId);
    }

    public void setTitleText(String text)
    {
        this.text_title.setText(text);
    }

    public void setTitleTextColor(int color)
    {
        this.text_title.setTextColor(color);
    }

    public void setLeftVisible(boolean visible)
    {
        if (visible)
        {
            this.button_left.setVisibility(View.VISIBLE);
        } else
        {
            this.button_left.setVisibility(View.GONE);
        }
    }

    public void setOnLeftButtonClickListener(OnClickListener listener)
    {
        this.button_left.setOnClickListener(listener);
    }

    public void setRightVisible(boolean visible)
    {
        if (visible)
        {
            this.button_right.setVisibility(View.VISIBLE);
        } else
        {
            this.button_right.setVisibility(View.GONE);
        }
    }

    public void setOnRightButtonClickListener(OnClickListener listener)
    {
        this.button_right.setOnClickListener(listener);
    }

    public void setBackgroundColor(int color)
    {
        mainView.setBackgroundColor(color);
    }
}
